home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / libs / intuisup.lha / Intuisup / source.lha / Editor / subs.c < prev    next >
C/C++ Source or Header  |  1992-10-05  |  7KB  |  376 lines

  1. /* $Revision Header *** Header built automatically - do not edit! ***********
  2.  *
  3.  *    (C) Copyright 1991 by Torsten Jürgeleit
  4.  *
  5.  *    Name .....: subs.c
  6.  *    Created ..: Sunday 22-Dec-91 21:23:09
  7.  *    Revision .: 0
  8.  *
  9.  *    Date        Author                 Comment
  10.  *    =========   ====================   ====================
  11.  *    22-Dec-91   Torsten Jürgeleit      Created this file!
  12.  *
  13.  ****************************************************************************
  14.  *
  15.  *    Subroutines
  16.  *
  17.  * $Revision Header ********************************************************/
  18.  
  19.  /* Includes */
  20.  
  21. #include "includes.h"
  22. #include "defines.h"
  23. #include "imports.h"
  24. #include "protos.h"
  25.  
  26.  /* Draw box in given window */
  27.  
  28. VOID
  29. draw_box(struct Window * win, struct Box * box)
  30. {
  31.     struct RastPort *rp = win->RPort;
  32.  
  33.     SetDrMd(rp, (LONG) COMPLEMENT);
  34.     Move(rp, (LONG) box->bo_X1, (LONG) box->bo_Y1);
  35.     Draw(rp, (LONG) box->bo_X2, (LONG) box->bo_Y1);
  36.     Draw(rp, (LONG) box->bo_X2, (LONG) box->bo_Y2);
  37.     Draw(rp, (LONG) box->bo_X1, (LONG) box->bo_Y2);
  38.     Draw(rp, (LONG) box->bo_X1, (LONG) box->bo_Y1);
  39. }
  40.  
  41.  /* Draw box in given window with border of 1 pixel */
  42.  
  43. VOID
  44. draw_box_with_border(struct Window *win, struct Box *box)
  45. {
  46.     struct RastPort *rp = win->RPort;
  47.  
  48.     SetDrMd(rp, (LONG) COMPLEMENT);
  49.     Move(rp, (LONG) (box->bo_X1 - 1), (LONG) (box->bo_Y1 - 1));
  50.     Draw(rp, (LONG) (box->bo_X2 + 1), (LONG) (box->bo_Y1 - 1));
  51.     Draw(rp, (LONG) (box->bo_X2 + 1), (LONG) (box->bo_Y2 + 1));
  52.     Draw(rp, (LONG) (box->bo_X1 - 1), (LONG) (box->bo_Y2 + 1));
  53.     Draw(rp, (LONG) (box->bo_X1 - 1), (LONG) (box->bo_Y1 - 1));
  54. }
  55.  
  56.  /* Draw box in given window with offset */
  57.  
  58. VOID
  59. draw_box_with_offset(struct Window *win, struct Box *box, SHORT xoffset,
  60.                      SHORT yoffset)
  61. {
  62.     struct RastPort *rp = win->RPort;
  63.  
  64.     SetDrMd(rp, (LONG) COMPLEMENT);
  65.     Move(rp, (LONG) (box->bo_X1 + xoffset), (LONG) (box->bo_Y1 + yoffset));
  66.     Draw(rp, (LONG) (box->bo_X2 + xoffset), (LONG) (box->bo_Y1 + yoffset));
  67.     Draw(rp, (LONG) (box->bo_X2 + xoffset), (LONG) (box->bo_Y2 + yoffset));
  68.     Draw(rp, (LONG) (box->bo_X1 + xoffset), (LONG) (box->bo_Y2 + yoffset));
  69.     Draw(rp, (LONG) (box->bo_X1 + xoffset), (LONG) (box->bo_Y1 + yoffset));
  70. }
  71.  
  72.  /* Get head of EXEC list */
  73.  
  74. VOID *
  75. get_head(struct List *list)
  76. {
  77.     struct Node *node = list->lh_Head;
  78.  
  79.     /* Check for empty list ? */
  80.     if (!node->ln_Succ)
  81.     {
  82.         node = NULL;
  83.     }
  84.     return ((VOID *) node);
  85. }
  86.  
  87.  /* Get tail of EXEC list */
  88.  
  89. VOID *
  90. get_tail(struct List * list)
  91. {
  92.     struct Node *node = list->lh_TailPred;
  93.  
  94.     /* Check for empty list ? */
  95.     if (!node->ln_Pred)
  96.     {
  97.         node = NULL;
  98.     }
  99.     return ((VOID *) node);
  100. }
  101.  
  102.  /* Get next node of EXEC list entry */
  103.  
  104. VOID *
  105. get_succ(struct Node * node)
  106. {
  107.     /* Check for end of list ? */
  108.     node = node->ln_Succ;
  109.     if (!node->ln_Succ)
  110.     {
  111.         node = NULL;
  112.     }
  113.     return ((VOID *) node);
  114. }
  115.  
  116.  /* Get previous node of EXEC list entry */
  117.  
  118. VOID *
  119. get_pred(struct Node * node)
  120. {
  121.     /* Check for start of list ? */
  122.     node = node->ln_Pred;
  123.     if (!node->ln_Pred)
  124.     {
  125.         node = NULL;
  126.     }
  127.     return ((VOID *) node);
  128. }
  129.  
  130.  /* Get node from list by given number */
  131.  
  132. VOID *
  133. get_node(struct List * list, USHORT num)
  134. {
  135.     struct Node *node;
  136.  
  137.     if (node = get_head(list))
  138.     {
  139.         while (num-- && (node = get_succ(node)));
  140.     }
  141.     return ((VOID *) node);
  142. }
  143.  
  144.  /* Duplicate text string */
  145.  
  146. SHORT
  147. duplicate_string(BYTE * text, BYTE ** ptr)
  148. {
  149.     LONG len;
  150.     SHORT status = EDITOR_STATUS_NORMAL;
  151.  
  152.     if (!text || (len = strlen(text) + 1) == 1)
  153.     {
  154.         *ptr = NULL;
  155.     }
  156.     else
  157.     {
  158.         if (!(*ptr = (BYTE *)malloc(len)))
  159.         {
  160.             status = EDITOR_ERROR_OUT_OF_MEM;
  161.         }
  162.         else
  163.         {
  164.             CopyMem(text, *ptr, len);
  165.         }
  166.     }
  167.     return (status);
  168. }
  169.  
  170.  /* Convert string to lower case */
  171.  
  172. VOID
  173. lower_string(BYTE * text, BYTE * ptr)
  174. {
  175.     if (ptr && text)
  176.     {
  177.         BYTE c;
  178.  
  179.         while ((c = *text++) != '\0')
  180.         {
  181.             *ptr++ = tolower(c);
  182.         }
  183.         *ptr = '\0';
  184.     }
  185. }
  186.  
  187.  /* Convert string to upper case */
  188.  
  189. VOID
  190. upper_string(BYTE * text, BYTE * ptr)
  191. {
  192.     if (ptr && text)
  193.     {
  194.         BYTE c;
  195.  
  196.         while ((c = *text++) != '\0')
  197.         {
  198.             *ptr++ = toupper(c);
  199.         }
  200.         *ptr = '\0';
  201.     }
  202. }
  203.  
  204.  /* Duplicate text list */
  205.  
  206. SHORT
  207. duplicate_text_list(struct Template *old_tp, struct Template *new_tp)
  208. {
  209.     struct Node *node;
  210.     SHORT status = EDITOR_STATUS_NORMAL;
  211.  
  212.     NewList(&new_tp->tp_TextList);
  213.     for (node = get_head(&old_tp->tp_TextList); node &&
  214.          status == EDITOR_STATUS_NORMAL; node = get_succ(node))
  215.     {
  216.         status = add_template_text_list_entry(new_tp, node->ln_Name);
  217.     }
  218.     return (status);
  219. }
  220.  
  221.  /* Build template text list from given text array */
  222.  
  223. SHORT
  224. build_template_text_list(struct Template * tp, BYTE ** text_array)
  225. {
  226.     BYTE *text;
  227.     SHORT status = EDITOR_STATUS_NORMAL;
  228.  
  229.     NewList(&tp->tp_TextList);
  230.     for (text = *text_array; text && status == EDITOR_STATUS_NORMAL;
  231.          text = *++text_array)
  232.     {
  233.         status = add_template_text_list_entry(tp, text);
  234.     }
  235.     if (status != EDITOR_STATUS_NORMAL)
  236.     {
  237.         free_template_text_list(tp);
  238.     }
  239.     return (status);
  240. }
  241.  
  242.  /* Build text list entry */
  243.  
  244. struct Node *
  245. build_text_list_entry(BYTE * text)
  246. {
  247.     struct Node *node;
  248.  
  249.     if (node = AllocMem((LONG) sizeof(struct Node), (LONG) MEMF_PUBLIC |
  250.                         MEMF_CLEAR))
  251.     {
  252.         if (!(node->ln_Name = (BYTE *)malloc((LONG) (strlen(text) + 1))))
  253.         {
  254.             FreeMem(node, (LONG) sizeof(struct Node));
  255.             node = NULL;
  256.         }
  257.         else
  258.         {
  259.             strcpy(node->ln_Name, text);
  260.         }
  261.     }
  262.     return (node);
  263. }
  264.  
  265.  /* Add text entry to template text list */
  266.  
  267. SHORT
  268. add_template_text_list_entry(struct Template * tp, BYTE * text)
  269. {
  270.     struct Node *node;
  271.     SHORT status = EDITOR_STATUS_NORMAL;
  272.  
  273.     if (!(node = build_text_list_entry(text)))
  274.     {
  275.         status = EDITOR_ERROR_OUT_OF_MEM;
  276.     }
  277.     else
  278.     {
  279.         AddTail(&tp->tp_TextList, node);
  280.     }
  281.     return (status);
  282. }
  283.  
  284.  /* Free template text list */
  285.  
  286. VOID
  287. free_template_text_list(struct Template * tp)
  288. {
  289.     struct List *list = &tp->tp_TextList;
  290.     struct Node *node;
  291.  
  292.     while (node = RemHead(list))
  293.     {
  294.         free_text_list_entry(node);
  295.     }
  296. }
  297.  
  298.  /* Free text list entry */
  299.  
  300. VOID
  301. free_text_list_entry(struct Node *node)
  302. {
  303.     free(node->ln_Name);
  304.     FreeMem(node, (LONG) sizeof(struct Node));
  305. }
  306.  
  307.  /* Build template text array from template text list */
  308.  
  309. SHORT
  310. build_template_text_array(struct Template *tp)
  311. {
  312.     struct List *list = &tp->tp_TextList;
  313.     struct Node *node;
  314.     BYTE **text_array;
  315.     USHORT count = 0;
  316.     SHORT status = EDITOR_STATUS_NORMAL;
  317.  
  318.     /* Count entries in text list */
  319.     for (node = get_head(list); node; node = get_succ(node))
  320.     {
  321.         count++;
  322.     }
  323.     if (!(text_array = (BYTE **)malloc((LONG) (count + 1) * sizeof(BYTE *))))
  324.     {
  325.         status = EDITOR_ERROR_OUT_OF_MEM;
  326.     }
  327.     else
  328.     {
  329.         struct GadgetData *gd = &tp->tp_Data.tp_GadgetData;
  330.  
  331.         switch (tp->tp_Type)
  332.         {
  333.         case TEMPLATE_TYPE_MX:
  334.             gd->gd_SpecialData.gd_MXData.gd_MXTextArray = text_array;
  335.             break;
  336.         case TEMPLATE_TYPE_CYCLE:
  337.             gd->gd_SpecialData.gd_CycleData.gd_CycleTextArray = text_array;
  338.             break;
  339.         }
  340.  
  341.         /* Fill text array with pointers to text list entries */
  342.         for (node = get_head(list); node && status == EDITOR_STATUS_NORMAL;
  343.              node = get_succ(node))
  344.         {
  345.             *text_array++ = node->ln_Name;
  346.         }
  347.         *text_array = NULL;        /* mark end of text array */
  348.     }
  349.     return (status);
  350. }
  351.  
  352.  /* Free template text array */
  353.  
  354. VOID
  355. free_template_text_array(struct Template * tp)
  356. {
  357.     struct GadgetData *gd = &tp->tp_Data.tp_GadgetData;
  358.     BYTE **text_array;
  359.  
  360.     switch (tp->tp_Type)
  361.     {
  362.     case TEMPLATE_TYPE_MX:
  363.         text_array = gd->gd_SpecialData.gd_MXData.gd_MXTextArray;
  364.         gd->gd_SpecialData.gd_MXData.gd_MXTextArray = NULL;
  365.         break;
  366.     case TEMPLATE_TYPE_CYCLE:
  367.         text_array = gd->gd_SpecialData.gd_CycleData.gd_CycleTextArray;
  368.         gd->gd_SpecialData.gd_CycleData.gd_CycleTextArray = NULL;
  369.         break;
  370.     default:
  371.         text_array = NULL;
  372.         break;
  373.     }
  374.     free(text_array);
  375. }
  376.